package cn.mesmile.lock.config;

import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zb
 * @date 2020/11/30 14:32
 * @Description
 */
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {


    @Autowired
    private RedissonProperties redssionProperties;
//  import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
//    @Autowired
//    private RedisProperties redisProperties;

    /**
     * 哨兵模式自动装配
     *      # 哨兵模式
     *      #redisson:
     *      #  master-name: mymaster
     *      #  password:
     *      #  sentinel-addresses: 10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381
     * @return
     */
//    @Bean
//    @ConditionalOnProperty(name="redisson.master-name")
//    RedissonClient redissonSentinel() {
//        Config config = new Config();
//        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
//                .setMasterName(redssionProperties.getMasterName())
//                .setTimeout(redssionProperties.getTimeout())
//                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
//                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
//
//        if(StrUtil.isNotBlank(redssionProperties.getPassword())) {
//            serverConfig.setPassword(redssionProperties.getPassword());
//        }
//        return Redisson.create(config);
//    }

    /**
     * 单机模式自动装配
     * @return
     */
    @Bean
    @ConditionalOnProperty(name="redisson.address")
    RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setDatabase(0)
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
        if(StrUtil.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }

        return Redisson.create(config);
    }


    /**
     *  redis集群下的配置  cluster
     * @return
     */
//    @Bean
//    public RedissonClient redisson() {
//        //redisson版本是3.5，集群的ip前面要加上“redis://”，不然会报错，3.2版本可不加
//        List<String> clusterNodes = new ArrayList<>();
//        for (int i = 0; i < redisProperties.getCluster().getNodes().size(); i++) {
//            clusterNodes.add("redis://" + redisProperties.getCluster().getNodes().get(i));
//        }
//        Config config = new Config();
//        // 采用集群模式
//        ClusterServersConfig clusterServersConfig = config.useClusterServers()
//                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
//        //设置密码  注意这里集群的 redis 密码都必须一致
//        clusterServersConfig.setPassword(redisProperties.getPassword());
//        return Redisson.create(config);
//    }

    /**
     * 装配locker类，并将实例注入到RedissLockUtil中
     * @return
     */
//    @Bean
//    RedissLockUtil redissLockUtil(RedissonClient redissonClient) {
//        RedissLockUtil redissLockUtil = new RedissLockUtil();
//        redissLockUtil.setRedissonClient(redissonClient);
//        return redissLockUtil;
//    }


}
